package com.six.backend.service.impl;

import com.six.backend.dao.AgendaDao;
import com.six.backend.dao.ConferenceDao;
import com.six.backend.dao.ThemeDao;
import com.six.backend.dao.ToolDao;
import com.six.backend.pojo.*;
import com.six.backend.service.ConferenceService;
import com.six.backend.utils.ListUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: skyler
 * @Date: 2021/11/4 15:25
 */
@Service
@RequiredArgsConstructor
public class ConferenceServiceImpl implements ConferenceService {
    @Autowired
    private final ConferenceDao conferenceDao;

    @Autowired
    private ThemeDao themeDao;

    @Autowired
    private AgendaDao agendaDao;

    @Autowired
    private ToolDao toolDao;

    @Override
    public int alterConference(Conference conference) {
        return conferenceDao.alterConference(conference);
    }

    @Override
    public List<Conference> getConference(Map map) {
        return conferenceDao.getConference(map);
    }

    @Override
    public Integer deleteConference(Map map) {
        return conferenceDao.deleteConference(map);
    }

    @Override
    public Boolean addConference(Conference conference) {
        // 将会议主持人以及纪要撰写人添加到参会人员中
        List<User> userList = conference.getUserList();
        User host = new User();
        host.setAccount(conference.getHostAccount());

        userList.add(host);
        User summaryWriter = new User();
        summaryWriter.setAccount(conference.getSummaryWriterAccount());
        userList.add(summaryWriter);
        List<String> userListAccount = new ArrayList<>();
        for (User user1 : userList) {
            userListAccount.add(user1.getAccount());
        }
        // 去除重复的人员
        List<String> list = ListUtil.removeDuplicate(userListAccount);
        List<User> userLists = new ArrayList<>();
        for (String s : list) {
            User user3 = new User();
            user3.setAccount(s);
            userLists.add(user3);
        }
        List<Agenda> agendaList = conference.getTheme().getAgendaList();
        List<Delphi> delphiList = new ArrayList<>();
        Tool tool = new Tool();
        tool.setAccount(conference.getHostAccount());
        tool.setDate(new Date());
        // 取出所有德尔菲问卷
        for (Agenda agenda : agendaList) {
            List<Delphi> delphi = agenda.getDelphi();
            toolDao.insertTool(tool);
            // 未议题装配工具编号
            agenda.setToolId(tool.getId());
            // 插入会议参会人员
            toolDao.insertParticipants(tool.getId(), userLists);
            // 为德尔菲问卷装配工具编号
            for (Delphi delphi1 : delphi) {
                delphi1.setToolId(tool.getId());
            }
            delphiList.addAll(delphi);
        }

        return  conferenceDao.insertConference(conference) == 1 &&
                conferenceDao.insertParticipants(conference.getId(), userLists) > 0 &&
                themeDao.insertTheme(conference.getTheme()) > 0 &&
                themeDao.insertConferenceThemeRelation(conference.getId(),conference.getTheme().getId()) == 1 &&
                agendaDao.insertAgenda(agendaList,conference.getTheme().getId()) > 0 &&
                toolDao.insertDelphi(delphiList) > 0;
    }

    @Override
    public Conference getConferenceInformation(Integer id) {
        Conference conferenceInformation = conferenceDao.getConferenceInformation(id);
        List<User> conferenceParticipants = conferenceDao.getConferenceParticipants(id);
        conferenceInformation.setUserList(conferenceParticipants);
        Theme  theme = conferenceDao.selectTheme(id);
        if (theme != null) {
            List<Agenda> agenda = conferenceDao.selectAgenda(theme.getId());
            theme.setAgendaList(agenda);
        }
        conferenceInformation.setTheme(theme);
        return conferenceInformation;
    }

    @Override
    public List<Agenda> selectAgenda(Integer id) {
        return conferenceDao.selectAgenda(id);
    }

    @Override
    public List<SourceMaterial> selectConferenceSourceById(Integer id) {
        return conferenceDao.selectConferenceSourceById(id);
    }

    @Override
    public List<SourceMaterial> selectConferenceSourceByName(Integer id, String name) {
        return conferenceDao.selectConferenceSourceByName(id,name);
    }

    @Override
    public Boolean startConference(Integer conferenceId) {
        return conferenceDao.startConference(conferenceId) == 1;
    }

    @Override
    public Boolean endConference(Integer conferenceId) {
        List<Agenda> agenda = conferenceDao.selectRunningAgenda(conferenceId);
        System.out.println(agenda);
        if (!agenda.isEmpty()) {
            return false;
        }
        return conferenceDao.endConference(conferenceId) == 1;
    }

    @Override
    public Boolean isConferenceHost(Integer conferenceId, String account) {
        return conferenceDao.isConferenceHost(conferenceId,account) != null;
    }

    @Override
    public Boolean isConferenceRunning(Integer conferenceId) {
        return conferenceDao.selectStatus(conferenceId) == 1;
    }

    @Override
    public void saveMessage(String roomName, String username, String data, Date date) {
        conferenceDao.saveMessage(roomName,username,data,date);
    }
}
